package cn.wizzer.app.web.modules.controllers.platform.gift;

import cn.wizzer.app.user.modules.services.GiftOrderService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.Arrays;

@IocBean
@At("/platform/gift/order/manager")
public class GiftOrderController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GiftOrderService giftOrderService;

    @At("")
    @Ok("beetl:/platform/gift/order/index.html")
    @RequiresPermissions("gift.order.manage")
    public void index() {

    }

    @At("/settlement")
    @Ok("json")
    @RequiresPermissions("gift.order.manage.settlement")
    @SLog(tag = "结算")
    public synchronized Object settlement(@Param("ids") String[] ids) {
        int sum = Arrays.stream(ids).mapToInt(id -> giftOrderService.settlement(id)).sum();
        return Result.success(sum==0? "无可结算数据":sum+"条操作成功");
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("gift.order.manage")
    public Object data(@Param("fromUser") String fromUser, @Param("toUser") String toUser, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, Integer sumToBalance) {
        return Result.success().addData(giftOrderService.data(pageNumber, pageSize, null,fromUser,toUser, sumToBalance));
    }


}
